 <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link rel="stylesheet" type="text/css" href="../js/themes/bootstrap/easyui.css">
	<link rel="stylesheet" type="text/css" href="../js/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo/demo.css">
	<script type="text/javascript" src="../js/jquery.min.js"></script>
	<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript">		
	$(function(){
	
	//	var ds="";
		var as="";


		
 		$.ajax({
				type:"GET",
		 		async: false,
				url:'<%=path%>/main/get/subinfo',
				dateType:'json',
				data:null,
				success:function(data){
					var result = eval('(' + data + ')');
					as=result.data;
				
					}
				}); 
		
		$('#tt').datagrid({
			
			width:525,
			height:350,
			url:'<%=path%>/st/selectst',
			singleSelect:true,
			queryParams:{
				cropid:<%=session.getAttribute("cropid")%>
			},
			columns:[[
			{field:'id',title:'序号',width:100,checkbox:true},
			{field:'settlementcode',title:'结算方式编码',width:100,editor: { type: 'textbox', options: { required: true } }},
			{field:'settlement',title:'结算方式',width:100,editor: { type: 'textbox', options: { required: true } }},
            {  
                field: 'subjectcode',  
                title: '科目编码',  
                width: 100,  
                formatter: function(value) {  
                    for (var i = 0; i < as.length; i++) {  
                        if (as[i].code == value){ 
                        	
                        	return as[i].code
                        };  
                    }  
                    return value;  
                },  
                editor: {  
                    type: 'combobox',  
                    options: {  
                        valueField: 'code',  
                        textField: 'code',  
                        data: as,  
                     
                        onSelect:function(rowIndex, rowData){
                  
                        	var row = $("#tt").datagrid('getSelected');
                        	var rindex = $("#tt").datagrid('getRowIndex', row);
                   
                        	var ed = $("#tt").datagrid('getEditor', {
                                index: rindex,
                                field: 'subjectcode'
                            });
                        	  var s = $(ed.target).combobox('getText');
                     	
                        	$.ajax({
                				type:"GET",
                		 		async: false,
                				url:'<%=path%>/main/get/subname',
                				dateType:'json',
                				data:{"subcode":s},
                				success:function(data){
                					
                					var result = eval('(' + data + ')');
                					var editor = $("#tt").datagrid("getEditor",{index:rindex,field:'subjectname'});
                	
                					$(editor.target).textbox('setValue',result.data.codename);
                					}
                				});
                        	}
                    }  
                }  
            },
			{field:'subjectname',title:'科目名称',width:100,editor: { type: 'textbox',  }},
	        {field:'action',title:'操作',width:90,align:'center',
                formatter:function(value,row,index){
                    if (row.editing){
                        var s = '<a href="#" onclick="saverow(this)">保存</a> ';
                        var c = '<a href="#" onclick="cancelrow(this)">取消</a>';
                        return s+c;
                    } else {
                        var e = '<a href="#" onclick="editrow(this)">编辑</a> ';
                        var d = '<a href="#" onclick="deleterow(this)">删除</a>';
                        return e+d;
                    }
                }
            }
			          ]],
			          onBeforeEdit:function(index,row){
				            row.editing = true;
				            updateActions(index);
				        },
				        onAfterEdit:function(index,row){
				            row.editing = false;
				            updateActions(index);
				        },
				        onCancelEdit:function(index,row){
				            row.editing = false;
				            updateActions(index);
				        }
		});	
	
		
		
	 	//点击 getcheck按钮获取  选中checkbox的一行值
		$('#btn1').click(function(){
			
			var checkedItems = $('#tt').datagrid('getChecked');
			
			var sde="";
			var st="";
			var sb="";
			var sn="";
			
			var guodu1="";
			var guodu2="";
		
			$.each(checkedItems, function(index, item){
				sde+=item.settlementcode+",";
				st+=item.settlement+",";
				
				if(item.subjectcode==""){
					guodu1="1,"
				}else{
					guodu1=item.subjectcode
				}
				
				sb+=guodu1;
						
				if(item.subjectname==""){
					guodu2="1,"
				}else{
					guodu2=item.subjectname
				}
				
				sn+=guodu2;

				});
			
	
		if(sde=="undefined,"||st=="undefined,"||sb=="undefined,"||sn=="undefined,"){
			alert("请填写数据");
		}else{
		 	$.ajax({
				type:"GET",
		 		async: false,
				url:'<%=path%>/st/insert/insertSettleStyle',
				dateType:'json',
				data:{"sde":sde,"st":st,"sb":sb,"sn":sn,"cropid":<%=session.getAttribute("cropid")%>},
				success:function(data){
					var result = eval('(' + data + ')');
					var num = result.status;
					if(num!=0){
						alert("保存成功");
						}
					}
				});
		}	
			});
	});
	function updateActions(index){
	    $('#tt').datagrid('updateRow',{
	        index: index,
	        row:{}
	    });
	}
	
	function getRowIndex(target){
	    var tr = $(target).closest('tr.datagrid-row');
	    return parseInt(tr.attr('datagrid-row-index'));
	}
	function editrow(target){
	    $('#tt').datagrid('beginEdit', getRowIndex(target));
	}
	function deleterow(target){
		var s=$("#tt").datagrid('getChecked');
	    $.messager.confirm('','你确定要删除吗？',function(r){
	        if (r){
	        	  
	       		  var scode="";
				  var subcode="";
					$.each(s, function(index, item){
					scode =item.settlementcode;
				
						});
					
				$.ajax({
					type:"GET",
			 		async: false,
					url:'<%=path%>/st/del',
					dateType:'json',
					data:{"scode":scode,"cropid":<%=session.getAttribute("cropid")%>},
					success:function(data){
						var num = data.status;
						if(num!=0){
							alert("删除成功!");	
						}
					}
					
				});	
				
	            $('#tt').datagrid('deleteRow', getRowIndex(target));
	          
	        }
	    });
	}
	function saverow(target){
	    $('#tt').datagrid('endEdit', getRowIndex(target));
	}
	function cancelrow(target){
	    $('#tt').datagrid('cancelEdit', getRowIndex(target));
	}
	
	function insert(){
		var row = $('#tt').datagrid('getSelected');
		if (row){
			var index = $('#tt').datagrid('getRowIndex', row);
		} else {
			index = 0;
		}
		$('#tt').datagrid('insertRow', {
			index: index,
			row:{
				status:'P'
			}
		});
		$('#tt').datagrid('selectRow',index);
		$('#tt').datagrid('beginEdit',index);
	}
	</script>
<style>
	#d1{
		text-align:center;
	}
#d3{
	width: 525px;
	margin: 45px auto;
}
#dd2{
	margin-left:100px;
	margin-bottom:20px;
	float:left;
}
</style>
<title>Insert title here</title>


</head>
<body>
		<div id="d1"><h2>结算方式信息表</h2></div>
		<div style="margin-left:570px;float:left" id="dd1">
			<span><a href="javascript:void(0)" class="easyui-linkbutton l-btn l-btn-small" onclick="insert()" >
				<span class="l-btn-left">
					<span class="l-btn-text">新增</span>
				</span>
			</a></span>
			<span><a href="javascript:void(0)" class="easyui-linkbutton l-btn l-btn-small">
				<span class="l-btn-left">
					<span class="l-btn-text" id='btn1'>保存</span>
				</span>
			</a></span>
		</div>

		<div id="d3">
			<table id="tt">
						
			</table>
		</div>
</body>
</html>